var express = require('express');
var router = express.Router();

var sql = require('../db/index');

/**
 * @swagger
 *       /customer/saveOrUpdate:
 *       post:
 *           description: 栏目新增
 *           tags: [顾客]
 *           produces:
 *               - application/x-www-form-urlencoded
 *           parameters:
 *               - name: id
 *                 description: 序号
 *                 required: true
 *                 type: number
 *               - name: number,
 *                 description: 栏目编号
 *                 required: true
 *                 type: string
 *               - name: name,
 *                 description: 名称
 *                 required: false
 *                 type: string
 *               - name: photo,
 *                 description: 图标
 *                 required: false
 *                 type: string
 *               - name: pnumber,
 *                 description: 排序号
 *                 required: false
 *                 type: string
 *               - name: product_category_id,
 *                 description: 操作
 *                 required: false
 *                 type: number
 *           responses:
 *               200:
 *                   description: 操作成功
 *               500:
 *                   description: 服务器错误
 */

// 新增或者修改接口  req.body
router.post('/saveOrUpdate', async (req, res) => {
  // 获取前端传递的参数 
  const { id, number, name, photo, pnumber, product_category_id } = req.body;
  // 如果id存在 修改操作 否则就是新增 
  let mysql = '';
  if (id) {
    mysql = `update jz_product set name = '${name}',description = '${description}',price ='${price}',
      photo = '${photo}',product_category_id=${product_category_id} where id=${id}
    `
  } else {
    mysql = `insert into jz_product (id,name,description,status,price,photo,product_category_id)
     values(null,'${name}','${description}','正常','${price}','${photo}',${product_category_id})`
  }
  await sql.query(mysql);
  // 给前端做出响应
  res.send({
    status: 200,
    message: "操作成功",
    data: null,
    timestamp: new Date().getTime()
  })
})

/**
 * @swagger
 *      /cloumn/changeStatus:
 *      post:
 *          description: 删除栏目
 *          tags: [栏目管理]
 *          parameters:
 *              - in: query
 *                name: id
 *                description: 栏目编号
 *                required: true
 *                schema:
 *                  type: number
 *              - in: query
 *                name: status
 *                description: 产品状态
 *                required: true
 *                schema:
 *                  type: string
 *          responses:
 *             '200':
 *                description: 返回产品列表
 */

// 上架和下架接口 req.body  需要前端传递参数 id status 
// id 是为了知道那个产品被修改状态 status 知道点击的 是下架或者上架 
router.post('/changeStatus', async (req, res) => {
    // 获取前端传递的参数 
    let { id, status } = req.body;
    if (status === '删除') {
      
    } else {
      
    }
    await sql.query(`update jz_product set status = '${status}' where id = ${id}`);
    // 给前端做出响应
    res.send({
      status: 200,
      message: status == '删除' ? '删除成功' : '',
      // if(status==='下架'){
      //   console.log('下架成功');
      // }else{
      //   console.log('上架成功');
      // }
      data: null,
      timestamp: new Date().getTime()
    })
  
  })
  
  
  /**
   * @swagger
   *    /cloumn/pageQuery:
   *    get:
   *        description: 分页多条件查询栏目列表
   *        tags: [顾客]
   *        parameters:
   *            - in: query
   *              name: page
   *              description: 页码
   *              required: true
   *              schema:
   *                type: number
   *            - in: query    
   *              name: pageSize
   *              description: 每页显示数量
   *              required: true
   *              schema:
   *                type: number
   *            - in: query    
   *              name: name
   *              description: 产品名称
   *              required: false
   *              schema:
   *                type: string
   *            - in: query    
   *              name: status
   *              description: 产品状态
   *              required: false
   *              schema:
   *                type: string
   *            - in: query    
   *              name: product_category_id
   *              description: 产品所属栏目
   *              required: false
   *              schema:
   *                type: number
   *        responses:
   *          '200':
   *            description: 返回产品列表
   */
  // 分页模糊查询接口 
router.get('/pageQuery', async function (req, res) {
    // 获取前端传递的参数 
    let { page, pageSize, name, status, product_category_id } = req.query;
    page = +page;
    pageSize = +pageSize;
    product_category_id = +product_category_id;
    let result, result1;
    // 每页查询条数
    let offset = (page - 1) * pageSize;
    if (name && status && product_category_id) {
      let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and status = '${status}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product where name like '${'%' + name + '%'}' and status = '${status}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name && status) {
      let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and status = '${status}' limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product where name like '${'%' + name + '%'}' and status = '${status}' limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name && product_category_id) {
      let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product where name like '${'%' + name + '%'}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (status && product_category_id) {
      let mysql = `select * from jz_product where status = '${status}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product where status = '${status}'  and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name || status || product_category_id) {
      if (name) {
        let mysql = `select * from jz_product where name like '${'%' + name + '%'}' limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where name like '${'%' + name + '%'}' limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      } else if (status) {
        let mysql = `select * from jz_product where status ='${status}' limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where status = '${status}' limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      } else {
        let mysql = `select * from jz_product where product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      }
    } else {
      // 分页查询 
      let mysql = `select * from jz_product  limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product  limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    }
    result = result.filter(item => {
      return item.length > 0;
    })
    console.log(result[0], '2222');
    total = result1[0][0].total;
    res.send({
      status: 200,
      message: '查询成功',
      data: result[0],
      total,
      timestamp: new Date().getTime()
    })
  })
  
  
  
  module.exports = router;
  
  